#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/2/26 15:21
# @Author  : yingxiao zhang
# @File    : ndx_ana.py

import pandas as pd

def calculate_max_drawdown(prices):
    # 初始化最大回撤和峰值
    max_drawdown = 0
    peak = prices[0]

    for price in prices:
        # 更新峰值
        if price > peak:
            peak = price
        # 计算当前回撤
        drawdown = (peak - price) / peak
        # 更新最大回撤
        if drawdown > max_drawdown:
            max_drawdown = drawdown

    return max_drawdown

data = pd.read_csv("ndx100.csv")

close_list = data['Close'].tolist()[::-1]
close_list = [float(item.replace(',', '')) for item in close_list]


one_rate = (close_list[0] - close_list[12])/close_list[12]
three_rate = (close_list[0] - close_list[12*3])/close_list[12*3]
five_rate = (close_list[0] - close_list[12*5])/close_list[12*5]
ten_rate = (close_list[0] - close_list[12*10])/close_list[12*10]
close_list = close_list[:120]
close_list = close_list[::-1]
max_drawdown = calculate_max_drawdown(close_list)
print(one_rate, three_rate, five_rate, ten_rate)
print(max_drawdown)
data_list = close_list[:360][::12][::-1]
print(data_list)
print(len(data_list))
print(data_list[0], data_list[15], data_list[-1])

def calculate_average_annual_growth_rate(data):
    """
    计算给定数据列表的平均年化增长率
    :param data: 数据列表
    :return: 平均年化增长率
    """
    n = len(data) - 1
    if n == 0:
        return 0
    total_growth = 1
    for i in range(1, len(data)):
        growth_rate = (data[i] - data[i - 1]) / data[i - 1]
        total_growth *= (1 + growth_rate)
    return total_growth ** (1 / n) - 1

# 分割数据为前 15 个和后 15 个
first_15 = data_list[:15]
last_15 = data_list[15:]

# 计算前 15 个数值的平均年化增长率
first_15_annual_growth = calculate_average_annual_growth_rate(first_15)

# 计算后 15 个数值的平均年化增长率
last_15_annual_growth = calculate_average_annual_growth_rate(last_15)

print(f"前 15 个数值的平均年化增长率: {first_15_annual_growth * 100:.2f}%")
print(f"后 15 个数值的平均年化增长率: {last_15_annual_growth * 100:.2f}%")